// app.js
import {
  code2session
} from "./utils/api"
App({
  onLaunch() {
    wx.getStorage({
      key: "token",
      success: (res) => {
        console.log(res)
      },
      fail: (error) => {
        wx.showModal({
          title: '请登录',
          content: '点击确定登录',
          complete: (res) => {
            if (res.cancel) {
              wx.exitMiniProgram()
            }
            if (res.confirm) {
              wx.login({
                success: (res) => {
                  console.log(res.code)
                  code2session({
                    code: res.code
                  }).then(result => {
                    console.log(result);
                    wx.setStorageSync("token", result.data.data)
                    wx.reLaunch({
                      url: '/pages/personal/personal',
                    })
                  })
                },
              })
            }
          }
        })

      }
    })
    wx.cloud.init({
      env: 'prod-4gnshh372cb1b689',
      traceUser: true
    })
  },
  globalData: {
    userInfo: null
  }
})